package org.dubnation.user.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.dubnation.user.domain.User;
import org.dubnation.user.rabbitmq.config.RegisterNoticeRabbitmqConfig;
import org.elasticsearch.common.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;


/**
 * 测试mq
 */
@RestController
@Slf4j
public class AmqpTestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 使用通配符模式发送消息
     * @return
     */
    @PostMapping("/testAmqp")
    public String testMq() {
        User user = new User();
        user = user.setId(30L).setUsername("Stephen");
        //设置消息唯一id
        String messageId = user.getId().toString();
        CorrelationData correlationData = new CorrelationData(messageId);
        log.info("====消息发送-->消息id为{}====", messageId);
        //生产者将消息发送到交换机中, 由交换机根据routingkey将消息转发给绑定的队列
        rabbitTemplate.convertAndSend(RegisterNoticeRabbitmqConfig.EXCHANGE_TOPICS_TEST, "test", user, correlationData);
        return "ok";
    }
}
